/*

 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package org.springblade.modules.system.controller;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.modules.resource.entity.Attach;
import org.springblade.modules.resource.service.IAttachService;
import org.springblade.modules.system.config.MesConfig;
import org.springblade.modules.system.domain.BladeFile;
import org.springblade.modules.system.util.QpUploadUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 对象存储端点
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@Api(value = "对象存储端点", tags = "对象存储端点")
@RequestMapping(AppConstant.APPLICATION_RESOURCE_NAME + "/sys/file")
public class ResourceController {
	@Resource
	MesConfig fileConfig;
	/**
	 * 附件表服务
	 */
	private final IAttachService attachService;


	/**
	 * 下载文件
	 *
	 * @param fileId:   文件id
	 * @param response: 相应
	 * @throws Exception：异常
	 */
	@RequestMapping(path = "/download/{fileId}", method = RequestMethod.GET)
	public void download(@PathVariable String fileId, HttpServletResponse response) throws Exception {
		Attach fileInfo = this.attachService.getById(fileId);
		if (fileInfo == null) {
			QpUploadUtil.buildResponse(response, "文件不存在");
			return;
		}
		String fileName = fileInfo.getName();
		String filePath = fileInfo.getDomainUrl();
		try {
			File file = QpUploadUtil.downFile(fileConfig, fileInfo.getUploadType(), fileName, filePath);
			if (file == null || !file.exists()) {
				QpUploadUtil.buildResponse(response, "文件不存在");
				return;
			}
			response.reset();
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1"));
			OutputStream fileOut = response.getOutputStream();
			fileOut.write(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
			fileOut.flush();
		} catch (Exception ex) {
			ex.printStackTrace();
			QpUploadUtil.buildResponse(response, "导出异常，" + ex.getMessage());
		}
	}


	/**
	 * 上传文件并保存至附件表
	 *
	 * @param file 文件
	 * @return ObjectStat
	 */
	@SneakyThrows
	@PostMapping("/uploadResource")
	public R<BladeFile> uploadResource(@RequestParam MultipartFile file) {
		String fileName = file.getOriginalFilename();
		Attach attach = QpUploadUtil.putFile(fileConfig, MesConfig.UPLOAD_TYPE_RESOURCE, fileName, file.getInputStream());
		attachService.save(attach);
		return R.data(QpUploadUtil.convert(attach));
	}

	/**
	 * 上传文件并保存至附件表
	 *
	 * @param file 文件
	 * @return ObjectStat
	 */
	@SneakyThrows
	@PostMapping("/upload")
	public R<BladeFile> upload(@RequestParam MultipartFile file) {
		String fileName = file.getOriginalFilename();
		Attach attach = QpUploadUtil.putFile(fileConfig, MesConfig.UPLOAD_TYPE_FILE, fileName, file.getInputStream());
		attachService.save(attach);
		return R.data(QpUploadUtil.convert(attach));
	}


	/**
	 * 删除文件
	 *
	 * @return R
	 */
	@SneakyThrows
	@PostMapping("/remove")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	public R removeFile(@RequestParam String fileId) {
		Attach fileInfo = this.attachService.getById(fileId);
		if (fileInfo == null) {
			return R.fail("文件不存在");
		}
		attachService.removeById(Long.valueOf(fileId));
		QpUploadUtil.removeFiles(fileConfig, fileInfo.getUploadType(), fileInfo.getName());
		return R.success("操作成功");
	}


}
